#/bin/python
import commands
import MySQLdb
import sys

xml="truefalseUmai:PROG/783734@BESTV.SMG.SMGfalse10Umai:PROG/775590@BESTV.SMG.SMGfalse10Umai:PROG/775637@BESTV.SMG.SMGfalse10Umai:PROG/773127@BESTV.SMG.SMGfalse10Umai:PROG/781747@BESTV.SMG.SMGfalse10Umai:PROG/770875@BESTV.SMG.SMGfalse10Umai:PROG/782248@BESTV.SMG.SMGfalse5Umai:PROG/778892@BESTV.SMG.SMGfalse10Umai:PROG/774465@BESTV.SMG.SMGfalse10Umai:PROG/782712@BESTV.SMG.SMGfalse5Umai:PROG/772978@BESTV.SMG.SMGfalse10Umai:PROG/767558@BESTV.SMG.SMGfalse10Umai:PROG/782139@BESTV.SMG.SMGfalse10Umai:PROG/775977@BESTV.SMG.SMGfalse10Umai:PROG/784190@BESTV.SMG.SMGfalse5Umai:PROG/761079@BESTV.SMG.SMGfalse5Umai:PROG/756493@BESTV.SMG.SMGfalse5Umai:PROG/360324@BESTV.SMG.SMGfalse4Umai:PROG/772998@BESTV.SMG.SMGfalse10Umai:PROG/653122@BESTV.SMG.SMGfalse5120"
cmd="echo " + xml + " | perl -pe 's/(BESTV.SMG.SMG)[^U]*/$1/g' | perl -pe 's/[^G]*(Umai)/$1/g' | sed 's/GU/G,U/g'"
lst=commands.getoutput(cmd).split(",")
output=",".join([('"' + str(ele) + '"') for ele in lst])

# connect to MySQL database
try:
	conn = MySQLdb.connect(host='10.4.190.9', user='root', passwd='mysql', db='recommendationengine', init_command='set names utf8')
	cursor = conn.cursor()
except MySQLdb.Error, e:
	print "Error %d: %s" % (e.args[0], e.args[1])
	sys.exit (1)

# query for program name
program = {}

sql = "select programid,name from program where programid in (%s)" % (output)
cursor.execute(sql)
rows = cursor.fetchall()

for row in rows:
	program[row[0]] = row[1]

sql = "select seriesid,name from series where seriesid in (%s)" % (output)
cursor.execute(sql)
rows = cursor.fetchall()

for row in rows:
        program[row[0]] = row[1]
# query completed

for i in program:
    print '%s\t%s' % (i, program[i])
